package com.honour.controller.interfaced;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.business.service.interfaced.ApplyForInManager;
import com.business.service.interfaced.impl.HighEntertainInService;
import com.honour.service.interfaced.BootPageInManager;
import com.honour.service.interfaced.SortInManager;
import com.honour.service.interfaced.UserInManager;
import com.honour.service.interfaced.impl.DiscoverInService;
import com.honour.service.interfaced.impl.EnergyInService;
import com.live.service.interfaced.impl.LiveInService;
import com.system.controller.base.BaseController;
import com.system.redis.RedisDao;
import com.system.util.AliSendPhoneUtil;
import com.system.util.AppUtil;
import com.system.util.Const;
import com.system.util.DateUtil;
import com.system.util.FTPUtils;
import com.system.util.MD5;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.UpdImgUtils;


/**
 * @Function
 * @Author 于泓磊
 * @Date 2017年10月19日
 */
@Controller
@RequestMapping(value = "/appuserin")
public class UserInController extends BaseController {

	@Resource(name = "userinService")
	private UserInManager userinService;

	@Resource(name = "energyInService")
	private EnergyInService energyInService;
	
	@Resource(name="bootpageinService")
	private BootPageInManager bootpageinService;
	
	@Resource(name="discoverinService")
	private DiscoverInService discoverinService;
	
	@Resource(name="liveInService")
	private LiveInService liveInService;
	@Resource(name="applyforInService")
	private ApplyForInManager applyforInService;
	
	@Resource(name="sortinService")
	private SortInManager sortinService;
	
	@Resource(name = "redisDaoImpl")
	private RedisDao redisDaoImpl;
	
	@Resource(name="highEntertainInService")
	private HighEntertainInService highEntertainInService; 

	/**
	 * 用户注册
	 * 
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @return
	 */
	@RequestMapping(value = "/userregister")
	@ResponseBody
	public Object userRegister() {
		logBefore(logger, "用户注册接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("REGISTER", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("UserRegister", pd)) { // 检查参数
					pd.put("HONOURUSER_ID", this.getSnowId());
					int INITPASSWORD = Tools.getRandomNum(); // 初始密码
					pd.put("INITPASSWORD", INITPASSWORD);
					String SALT = DateUtil.getdaySFM(); // 生成盐
					String password = MD5.getPassword(INITPASSWORD + SALT); // 对新密码加盐
					pd.put("PASSWORD", password);
					pd.put("SALT", SALT);
					userinService.save(pd);
					//用户注册成功后初始化头条我的菜单
					pd.put("USERTOPLINE_ID", this.getSnowId());
					List<PageData> danList = discoverinService.getCaiDanList(pd);
					String DISCOVERVIDEO_IDS="";
					for (PageData p : danList) {
						DISCOVERVIDEO_IDS=","+p.getString("CAIDAN_ID");
					}
					if(Tools.notEmpty(DISCOVERVIDEO_IDS)){
						DISCOVERVIDEO_IDS=DISCOVERVIDEO_IDS.substring(1);
					}
					pd.put("DISCOVERVIDEO_IDS", DISCOVERVIDEO_IDS);
					discoverinService.addUserTopLine(pd);
					result = (null == pd) ? "02" : "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 用户手机号登录接口(暂废弃)
	 * 
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @return
	 */
	/*
	 * @RequestMapping(value="/userloginphone")
	 * 
	 * @ResponseBody public Object userLoginPhone(HttpSession session){
	 * logBefore(logger, "用户手机号登录接口"); Map<String,Object> map = new
	 * HashMap<String,Object>(); PageData pd = new PageData(); pd =
	 * this.getPageData(); String result = "00"; try{ if(Tools.checkKey("LOGIN",
	 * pd.getString("FKEY"))){ //检验请求key值是否合法
	 * if(AppUtil.checkParam("userLoginPhone", pd)){ //检查参数 pd=
	 * userinService.userLoginByPhone(pd); map.put("pd", pd);
	 * //返回01表示登录成功，返回04表示用户名或密码错误 result = (null == pd) ? "04" : "01"; }else {
	 * result = "03"; } }else{ result = "05"; } }catch (Exception e){
	 * logger.error(e.toString(), e); }finally{ map.put("result", result);
	 * logAfter(logger); } return AppUtil.returnObject(new PageData(), map); }
	 */

	/**
	 * 用户卡号密码登录接口
	 * 
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @return
	 */
	@RequestMapping(value = "/userlogincard")
	@ResponseBody
	public Object userLoginCard() {
		logBefore(logger, "用户卡号密码登录接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("LOGIN", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userLoginCard", pd)) { // 检查参数
					PageData user = userinService.userLoginByCard(pd);
					if (user != null) {
						String a = pd.getString("PASSWORD") + user.getString("SALT");
						String PASSWORD = MD5.getPassword(a); // 对密码加盐
						if (PASSWORD.equals(user.getString("PASSWORD"))) { // 用户输出的密码和数据库对比
							user.remove("SALT"); // 给用户返回的信息中将盐移除
							map.put("pd", user);
							result = "01";
						} else {
							result = "06"; // 密码错误
						}
					} else {
						result = "04"; // 没有该卡号
					}
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取用户收货地址列表接口
	 * 
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @return
	 */
	@RequestMapping(value = "/usershipadr")
	@ResponseBody
	public Object userShipAdr(HttpSession session) {
		logBefore(logger, "获取用户收货地址列表接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("SHIPADR", pd.getString("FKEY"))) { // 检验请求key值是否合法
				List<PageData> list = userinService.getUserShipAdr(pd);
				if (list.size() != 0) {
					map.put("pd", list);
				}
				result = (0 == list.size()) ? "02" : "01";
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 添加用户收货地址
	 * (先不用这个接口,用OrderController中对应的添加用户收货地址接口)
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @return
	 */
	@RequestMapping(value = "/useraddshipadr")
	@ResponseBody
	public Object userAddShipAdr(HttpSession session) {
		logBefore(logger, "添加用户收货地址接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("SHIPADR", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userAddShipAdr", pd)) { // 检查参数
					pd.put("ADDRESS_ID", this.getSnowId());
					userinService.addUserShipAdr(pd);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 删除用户收货地址接口
	 * 
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/userdelshipadr")
	@ResponseBody
	public Object userDelShipAdr(HttpSession session) {
		logBefore(logger, "删除用户收货地址接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("SHIPADR", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userDelShipAdr", pd)) { // 检查参数
					userinService.delUserShipAdr(pd);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 修改用户收货地址接口
	 * (暂不用此接口)
	 * @Author 于泓磊
	 * @date 2017年10月19日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/useredishipadr")
	@ResponseBody
	public Object userEdiShipAdr(HttpSession session) {
		logBefore(logger, "修改用户收货地址接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("SHIPADR", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userEdiShipAdr", pd)) { // 检查参数
					userinService.editUserShipAdr(pd);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获得用户名下所有订单列表
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年11月19日
	 * @return
	 */
	@RequestMapping(value = "/userorderalllist")
	@ResponseBody
	public Object getAllUserOrderList() {
		logBefore(logger, "获得用户名下所有订单列表接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("ALLUSERORDER", pd.getString("FKEY"))) { // 检验请求key值是否合法
				List<PageData> list = userinService.getUserAllOrderList(pd);
				if (list.size() != 0) {
					map.put("pd", list);
				}
				result = (0 == list.size()) ? "02" : "01";
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 上传用户头像方法(加同步锁)
	 * 
	 * @Author 于泓磊
	 * @date 2017年11月24日
	 * @param req
	 * @param multiReq
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/up")
	@ResponseBody
	public synchronized Object uploadimages(HttpServletRequest req, MultipartHttpServletRequest multiReq,
			HttpSession session, String HONOURUSER_ID) throws Exception {
		logBefore(logger, "用户上传头像接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("UPPORT", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (!"".equals(HONOURUSER_ID) && HONOURUSER_ID != null) { // 检查参数
					// 获取上传文件的路径
					String uploadFilePath = multiReq.getFile("showfile").getOriginalFilename();
					// 截取上传文件的后缀
					String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1,
							uploadFilePath.length());
					// 用UUID来解决同名文件
					String upload = UUID.randomUUID().toString().replace("-", "");
					// 获取传输过来的Input流
					InputStream is = multiReq.getFile("showfile").getInputStream();
					// 根据txt文件读取ftp工具类上传需要的参数
					String updeploy = Tools.readTxtFileAll(Const.UPDOWNLOADPATH, "UTF-8");
					String[] split = updeploy.split(",");
					// 第一个参数为 图片服务器的地址
					// 第二个参数为 图片服务器接口
					// 第三/四个参数为 FTP服务器的登录用户名和密码
					String path = "userportrait";
					FTPUtils.uploadFile(split[0], Integer.parseInt(split[1]), split[2], split[3], path,
							upload + "." + uploadFileSuffix, is);
					// 通过工具类获取图片服务器地址
					String imgpath = Tools.readTxtFileAll(Const.IMAGEPATH, "UTF-8");
					String PROTRAIT = imgpath + path + "/" + upload + "." + uploadFileSuffix;
					pd = this.getPageData();
					// 根据用户id删除用户之前的头像数据
					pd.put("HONOURUSER_ID", HONOURUSER_ID);
					PageData p = userinService.getUserById(pd);
					UpdImgUtils.updImgMethod(p, pd, PROTRAIT, "PORTRAIT", path);
					pd.put("PORTRAIT", PROTRAIT);
					pd.put("HONOURUSER_ID", HONOURUSER_ID);
					userinService.updUserPortrait(pd);
					//获取当前用户是否已经完成头像上传，如果完成，则任务更新
					pd = liveInService.getTaskStatus(pd);
					PageData page= userinService.getUserPortrait(pd);
					if (page.getString("PORTRAIT")!=null&&page.getString("PORTRAIT")!="") {
						//如果数据库中该任务处于未完成状态，则改变状态并加上相对应的黑咖币
						if (pd.getInterger("TASK_PORT")==0) {
							//修改任务状态
							pd.put("TASK_PORT", 1);
							//添加对应的黑咖币数量
							pd.put("BLACKCOIN", 20);
							liveInService.updTaskStatus(pd);
						}
					}
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 修改用户个人资料
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月07日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/userpersonalinfo")
	@ResponseBody
	public Object userEdiShipPerInfo(HttpSession session) {
		logBefore(logger, "修改用户个人资料");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("UPDPERSONALINFO", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("sDistance", pd)) { // 检查参数
					userinService.updPersonalInfo(pd);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取用户的个人信息
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月07日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getuserinfo")
	@ResponseBody
	public Object getUserInfo() {
		logBefore(logger, "获取用户的个人信息");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("GETUSERINFO", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("sDistance", pd)) { // 检查参数
					PageData user = userinService.getUserById(pd);
					map.put("user", user);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 修改用户密码
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月11日
	 * @return
	 */
	@RequestMapping(value = "/userupdpassword")
	@ResponseBody
	public Object updUserPassword() {
		logBefore(logger, "修改用户密码");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("UPDUSERPASSWORD", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("updUserPassword", pd)) { // 检查参数
					PageData user = userinService.getUserById(pd);
					// 修改用户密码(没有忘记密码)
					if (pd.getString("OLDPASSWORD") != null && !"".equals(pd.getString("OLDPASSWORD"))) {
						String a = pd.getString("OLDPASSWORD") + user.getString("SALT");
						String oldpassword = MD5.getPassword(a); // 对旧密码加盐
						if (oldpassword.equals(user.getString("PASSWORD"))) {
							String SALT = DateUtil.getdaySFM();
							String password = MD5.getPassword(pd.getString("PASSWORD") + SALT); // 对新密码加盐
							user.put("PASSWORD", password);
							user.put("SALT", SALT);
							userinService.updPersonalInfo(user);
							result = "01";
						} else {
							result = "06"; // 旧密码错误
						}
					} else { // 修改用户密码(忘记密码)
						String SALT = DateUtil.getdaySFM();
						String password = MD5.getPassword(pd.getString("PASSWORD") + SALT); // 对新密码加盐
						user.put("PASSWORD", password);
						user.put("SALT", SALT);
						userinService.updPersonalInfo(user);
						result = "01";
					}
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取用户会员中心信息
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月16日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getmemberinfo")
	@ResponseBody
	public Object getMemberInfo() {
		logBefore(logger, "获取用户会员中心信息");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("MEMBERINFO", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("sDistance", pd)) { // 检查参数
					PageData memberInfo = userinService.getMemberInfo(pd);
					// 获取用户会员中心会员福利信息
					List<PageData> list = userinService.getWelfareInfo(memberInfo);
					map.put("memberInfo", memberInfo);
					map.put("list", list);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取我的优惠券，黑咖币，能量值
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月16日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getmycoublackenergy")
	@ResponseBody
	public Object getMyCouBlackEnergy() {
		logBefore(logger, "获取用户会员中心信息");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("MYCOUBLACKENERGY", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("sDistance", pd)) { // 检查参数
					PageData my = userinService.getMyCouBlackEnergy(pd);
					PageData energyScore = userinService.getEnergyScore(pd);
					Integer energy = energyScore.getInterger("SHOPSCORE") + energyScore.getInterger("ACTIVITYSCORE")
							+ energyScore.getInterger("INTERACTSCORE") + energyScore.getInterger("BASISSCORE")
							+ energyScore.getInterger("HONOURSCORE");
					my.put("ENERGY", energy);
					map.put("my", my);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 找回登录密码 (忘记密码)
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月16日
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getuserbyforget")
	@ResponseBody
	public Object getUserByForget() {
		logBefore(logger, "找回登录密码 (忘记密码)");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("USERBYFORGET", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("getUserByForget", pd)) { // 检查参数
					PageData user = userinService.getUserByForget(pd);
					map.put("pd", user);
					result = (null == user) ? "02" : "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 修改用户手机号码
	 * 
	 * @Author zhaozhenhua
	 * @date 2017年12月20日
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/upduserphone")
	@ResponseBody
	public Object updUserPhone() {
		logBefore(logger, "修改用户手机号码");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("UPDUSERPHONE", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("updUserPhone", pd)) { // 检查参数
					userinService.updPersonalInfo(pd);
					result = "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 用户网页版注册
	 * 
	 * @Author 于泓磊
	 * @date 2018年1月5日
	 * @return
	 */
	@RequestMapping(value = "/userregisterweb")
	@ResponseBody
	public Object userRegisterWeb() {
		logBefore(logger, "用户网页版注册接口");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("REGISTERWEB", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("UserRegister", pd)) { // 检查参数
					// 获取网页传输过来的小卡类名称
					Integer cardtypename = Integer.parseInt((String) pd.get("CARDTYPENAME")) ;
					Integer suiji = Integer.parseInt((String) pd.get("SUIJI"));
					switch (suiji) {
					case 1:
                        PageData p = bootpageinService.getRandomCardNum(pd);
                        pd.put("CARDNO", p.get("CARDNUM"));
                        //将随机出的卡号变为已经使用
                        p.put("CARDTYPE_ID", 1);
                        bootpageinService.updRandomCardNum(p);
						break;
					case 2:
						break;
					default:
						break;
					}
					switch (cardtypename) {
					case 1:
						pd.put("CARDTYPE", "390181853778149376");
						break;
					case 2:
						pd.put("CARDTYPE", "390200265979646133");
						break;
					case 3:
						pd.put("CARDTYPE", "390201795059646464");
						break;
					case 4:
						pd.put("CARDTYPE", "390201950420860928");
						break;
					case 7:
						pd.put("CARDTYPE", "390202047296700416");
						break;
					case 8:
						pd.put("CARDTYPE", "390202895984754688");
						break;
					case 9:
						pd.put("CARDTYPE", "390202951949352960");
						break;
					case 10:
						pd.put("CARDTYPE", "390202999533731840");
						break;
					case 11:
						pd.put("CARDTYPE", "390203316828635136");
						break;
					case 12:
						pd.put("CARDTYPE", "390203446780755968");
						break;
					case 13:
						pd.put("CARDTYPE", "390203482788855808");
						break;
					case 14:
						pd.put("CARDTYPE", "390203541202927616");
						break;
					case 15:
						pd.put("CARDTYPE", "390203583108218880");
						break;
					case 16:
						pd.put("CARDTYPE", "390203751593410560");
						break;
					case 17:
						pd.put("CARDTYPE", "390203784837464064");
						break;
					case 18:
						pd.put("CARDTYPE", "390203822426816512");
						break;
					case 19:
						pd.put("CARDTYPE", "390202180738482176");
						break;
					case 20:
						pd.put("CARDTYPE", "390202468161552384");
						break;
					case 21:
						pd.put("CARDTYPE", "390202526550458368");
						break;
					case 22:
						pd.put("CARDTYPE", "390202622746820608");
						break;
					case 23:
						pd.put("CARDTYPE", "390202686642847744");
						break;
					default:
						break;
					}
					pd.put("HONOURUSER_ID", this.getSnowId());
					int INITPASSWORD = Tools.getRandomNum(); // 初始密码
					pd.put("INITPASSWORD", INITPASSWORD);
					String SALT = DateUtil.getdaySFM(); // 生成盐
					String password = MD5.getPassword(INITPASSWORD + SALT); // 对新密码加盐
					pd.put("PASSWORD", password);
					pd.put("SALT", SALT);
					userinService.save(pd);
					result = (null == pd) ? "02" : "01";
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 后台用户登录
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年3月5日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value = "/backendlogin")
	@ResponseBody
	public Object backendLogin(HttpServletRequest request) {
		logBefore(logger, "后台用户登录");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try {
			if (Tools.checkKey("BACKENDLOGIN", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userLoginCard", pd)) { // 检查参数
					Object code = redisDaoImpl.get(Const.SESSION_SECURITY_CODE);
					System.out.println("code:"+code);
					System.out.println("codepd:"+pd.get("CODE"));
					if(code.toString().equalsIgnoreCase(pd.getString("CODE"))){
						PageData user = userinService.userLoginByCard(pd);
						if (user != null) {
							String a = pd.getString("PASSWORD") + user.getString("SALT");
							String PASSWORD = MD5.getPassword(a); // 对密码加盐
							if (PASSWORD.equals(user.getString("PASSWORD"))) { // 用户输出的密码和数据库对比
								user.remove("SALT"); // 给用户返回的信息中将盐移除
								map.put("user", user);
								PageData applyInfo = applyforInService.getApplyInfo(user);
								if(applyInfo!=null){
									PageData busi = new PageData();
									//根据入驻类型插入到相应的表中
									switch (applyInfo.getString("CATEGORY_ID")) {
									case "尊购商品":
										busi = sortinService.getBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "美食商家":
									    busi = sortinService.getFoodBusinessId(applyInfo);
									    if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "KTV":
										busi = sortinService.getKTVBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "酒吧":
										busi = sortinService.getBarBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "酒店商家":
										busi = sortinService.getHotelBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "马术商家":
										busi = highEntertainInService.getHorseBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "游艇商家":
										busi = highEntertainInService.getYachtBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "豪车商家":
										busi = highEntertainInService.getLuxcarBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "高尔夫商家":
										busi = highEntertainInService.getGolfBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									case "飞机商家":
										busi = highEntertainInService.getPlaneBusinessId(applyInfo);
										if(busi==null){
											result = "02";
											return AppUtil.returnObject(new PageData(), map);
										}
										applyInfo.put("BUSINESS_ID", busi.get("BUSINESS_ID"));
										break;
									default:
										break;
									}
								}
							    if(applyInfo!=null){
							    	map.put("applyInfo", applyInfo);	
							    }else{
							    	map.remove("user");
							    }
							    result=(applyInfo==null)?"02":"01";
							} else {
								result = "06"; // 密码错误
							}
						} else {
							result = "04"; // 没有该卡号
						}
					}else{
						result = "验证码错误";
					}
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 发送 手机短信验证码
	  * @Description
	  * @Author zhaozhenhua
	  * @date   2018年4月20日
	  * @param pd
	  * @return
	  * @throws Exception
	 */
	@RequestMapping(value = "/sendmsgcode")
	@ResponseBody
	public Object sendMsgCode(String PHONE) {
		logBefore(logger, "发送 手机短信验证码");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		System.out.println("phone:"+pd.getString("PHONE"));
		String result = "00";
		try {
			if (Tools.checkKey("SENDMSGCODE", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("userLoginPhone", pd)) { // 检查参数
					String size = redisDaoImpl.get(Const.ALI_CODE_SIZE+pd.getString("PHONE"));
					if(Tools.notEmpty(size)){
						if(Integer.parseInt(size)>=Const.ALI_CODE_SIZE){
							result = "请不要多次发送";
							return AppUtil.returnObject(new PageData(), map);
						}
					}
						
						
					String redisCode = redisDaoImpl.get(Const.ALI_CODE+pd.getString("PHONE"));
					System.out.println("redisCode:"+redisCode);
					if(Tools.isEmpty(redisCode)){
						HashMap<String, Object> alimsg = AliSendPhoneUtil.sendMessage(pd.getString("PHONE"));
						if(alimsg==null){
							result = "02";
						return AppUtil.returnObject(new PageData(), map);
					}
					if("2".equals(alimsg.get("Code"))){
						map.put("pd", "发送失败");
						return AppUtil.returnObject(new PageData(), map);
					}
					result = "01";
					}else {
						result = "请不要重复发送验证码";
					}
					
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}


}
